<template>
  <div>
    <div class="clearfix">
      <el-menu
        :default-active="activeIndex"
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
        background-color="#2a2d2f"
        text-color="#fff"
        active-text-color="#ffd04b"
        style="border-bottom: none;display: inline-block;float: left">
        <el-menu-item index="1">全部目标</el-menu-item>
        <el-menu-item index="2">有风险的目标</el-menu-item>
        <el-menu-item index="3">已完成的目标</el-menu-item>
      </el-menu>
      <el-button type="primary" style="float: right;border-radius: 0 4px 4px 0;margin-top: 10px;margin-right: 15px;" @click="SearchTarget">搜索目标</el-button>
      <el-input
        placeholder="搜索目标任务编号"
        v-model="searchTargetContent"
        prefix-icon="el-icon-search"
        clearable
        class="searchMission">
      </el-input>
    </div>

    <div style="background-color: white; margin: 20px;padding:20px;height: 500px;" >
        <el-table
          :data="TargetTable"
          style="width: 100%;"
          :header-cell-style="{background:'#eef1f6',color:'#606266'}">
          <el-table-column
            prop="status"
            label="状态"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="name"
            label="目标名称"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="principal"
            label="负责人"
            show-overflow-tooltip>
            <template slot-scope="scope">
              <el-avatar style="float:left; margin-top:3px;" size="small" :src="scope.row.circleUrl" v-if="scope.row.principalName !== '无'"></el-avatar>
              <span style="float:left;margin-left: 5px;margin-top: 8px">{{ scope.row.principalName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="targetValue"
            label="量化指标-目标值"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="currentValue"
            label="量化指标-当前值"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="overDate"
            label="截止时间"
            show-overflow-tooltip>
          </el-table-column>
        </el-table>
        <div style="margin-top: 10px;text-align: center">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 6, 7, 8]"
            :page-size="PageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
      </div>
    </div>
</template>

<script>
  const axios = require('axios');
  export default {
    name: "targetManagement",
    data() {
      return {
        activeIndex : '1',
        //分页数据
        currentPage: 1,
        total: 0,
        PageSize: 5,
        currentTable: 1,

        searchTargetContent: '',

        TargetTable:[],

      }
    },
    created() {
      this.$axios.all([
        axios.post('/getAllTarget', {"pageCode":this.currentPage,"pageSize":this.PageSize,"id":this.$route.params.id}),
        axios.post("/getProjectMessage",{"id":this.$route.params.id}),
      ]).then(this.$axios.spread((res1, res2) => {
        this.TargetTable = res1.data.list;
        this.total = res1.data.total;

        this.$parent.projectName = res2.data.projectName;
        this.$parent.topStatus = res2.data.topStatus;
        this.$parent.creatorId = res2.data.creatorId;
        this.$parent.principalId = res2.data.principalId;
      }))

    },
    methods: {
      handleSelect(key) {
        if(key == 1){
          axios.post('/getAllTarget', {"pageCode":this.currentPage,"pageSize":this.PageSize,"id":this.$route.params.id}).then(res => {
            this.TargetTable = res.data.list;
            this.total = res.data.total;
            this.currentPage = 1;
            this.currentTable = 1;
          })
        }else if(key == 2){
          axios.post('/getDangerMission', {"pageCode":this.currentPage,"pageSize":this.PageSize,"id":this.$route.params.id}).then(res => {
            this.TargetTable = res.data.list;
            this.total = res.data.total;
            this.currentPage = 1;
            this.currentTable = 2;
          })
        }else{
          axios.post('/getCompleteMission', {"pageCode":this.currentPage,"pageSize":this.PageSize,"id":this.$route.params.id}).then(res => {
            this.TargetTable = res.data.list;
            this.total = res.data.total;
            this.currentPage = 1;
            this.currentTable = 3;
          })
        }
      },
      handleCurrentChange(val) {
        this.currentPage = val;
        if(this.currentTable = 1){
          axios.post('/getAllTarget', {"pageCode":this.currentPage,"pageSize":this.PageSize,"id":this.$route.params.id}).then(res => {
            this.TargetTable = res.data.list;
            this.total = res.data.total;
            this.currentTable = 1;
          })
        }else if(this.currentTable = 2){
          axios.post('/getDangerMission', {"pageCode":this.currentPage,"pageSize":this.PageSize,"id":this.$route.params.id}).then(res => {
            this.TargetTable = res.data.list;
            this.total = res.data.total;
            this.currentTable = 2;
          })
        }else if(this.currentTable = 3){
          axios.post('/getCompleteMission', {"pageCode":this.currentPage,"pageSize":this.PageSize,"id":this.$route.params.id}).then(res => {
            this.TargetTable = res.data.list;
            this.total = res.data.total;
            this.currentTable = 3;
          })
        }else{
          axios.post('/getSearchAllTarget', {"pageCode":this.currentPage,"pageSize":this.PageSize,"id":this.$route.params.id,"MissionId":this.searchTargetContent}).then(res => {
            this.TargetTable = res.data.list;
            this.total = res.data.total;
            this.currentTable = 4;
          })
        }
      },
      handleSizeChange(val) {
        this.PageSize = val;
        if(this.currentTable = 1){
          axios.post('/getAllTarget', {"pageCode":this.currentPage,"pageSize":this.PageSize,"id":this.$route.params.id}).then(res => {
            this.TargetTable = res.data.list;
            this.total = res.data.total;
            this.currentTable = 1;
          })
        }else if(this.currentTable = 2){
          axios.post('/getDangerMission', {"pageCode":this.currentPage,"pageSize":this.PageSize,"id":this.$route.params.id}).then(res => {
            this.TargetTable = res.data.list;
            this.total = res.data.total;
            this.currentTable = 2;
          })
        }else if(this.currentTable = 3){
          axios.post('/getCompleteMission', {"pageCode":this.currentPage,"pageSize":this.PageSize,"id":this.$route.params.id}).then(res => {
            this.TargetTable = res.data.list;
            this.total = res.data.total;
            this.currentTable = 3;
          })
        }else{
          axios.post('/getSearchAllTarget', {"pageCode":this.currentPage,"pageSize":this.PageSize,"id":this.$route.params.id,"MissionId":this.searchTargetContent}).then(res => {
            this.TargetTable = res.data.list;
            this.total = res.data.total;
            this.currentTable = 4;
          })
        }
      },
      SearchTarget(){
        let reg = /^\d+$|^\d+[.]?\d+$/;
        if(this.searchTargetContent == ""){
          this.$message.error("搜索不能为空")
        }else if(!reg.test(this.searchTargetContent)) {
          this.$message.error("只能输入数字编号搜索")
        }else{
            axios.post('/getSearchAllTarget', {"pageCode":this.currentPage,"pageSize":this.PageSize,"id":this.$route.params.id,"MissionId":this.searchTargetContent}).then(res => {
              this.TargetTable = res.data.list;
              this.total = res.data.total;
              this.currentPage = 1;
              this.currentTable = 4;
            })
          }
        }
    }
  }
</script>

<style scoped>

</style>
